%% Initial tests

interval1 = IA_Interval('interval one', 1.1);
interval2 = IA_Interval('interval two', 2.5);

relation1 = IA_Relation([1 0 0 0 0 0 0; 0 0 0 0 0 0 0]);

network1 = IA_Network(interval1, interval2, relation1);

interval3 = IA_Interval('interval three', 3.7);
interval4 = IA_Interval('interval four', 4.8);

network1 = IA_Network_Add_Interval(network1, interval3);
network1 = IA_Network_Add_Interval(network1, interval4);

% test relation constraints function
relation2 = IA_Relation([1 1 1 1 1 1 1; 1 1 1 1 1 1 1]);
relation3 = IA_Relation_Constraints(relation1, relation2, IA_Relation_Dummy());

%% James F. Allen: Example
S = IA_Interval('S', 1);
L = IA_Interval('L', 1);
R = IA_Interval('R', 1);

Rel_SL = IA_Relation([0 0 1 1 0 0 0; 0 0 0 0 0 0 0]);
Rel_SR = IA_Relation([1 0 0 1 0 0 0; 1 0 0 1 0 0 0]);

Rel_SL_I_SR = IA_Relation_Intersect(Rel_SL, Rel_SR);
Rel_SL_U_SR = IA_Relation_Union(Rel_SL, Rel_SR);
Rel_SL_Inv = IA_Relation_Invert(Rel_SL);

network2 = IA_Network(S, L, Rel_SL);
network2 = IA_Network_Add_Interval(network2, R);
network2 = IA_Network_Add_Relation(network2, 'S', 'R', Rel_SR);

% continued story
Rel_LR = IA_Relation([0 0 1 0 1 0 0; 0 0 0 0 0 0 0]);
network3 = IA_Network_Add_Relation(network2, 'L', 'R', Rel_LR);

% final update
D = IA_Interval('D', 1);

Rel_DS = IA_Relation([0 1 0 0 0 0 0; 0 0 0 0 0 0 0]);
network4 = IA_Network_Add_Interval(network3, D);
network4 = IA_Network_Add_Relation(network4, 'D', 'S', Rel_DS);

%% Peter B. Ladkin: Check consistency
[scenario1, consistent1, max_depth1] = IA_Network_Check_Consistency(network1, 1, 2, 1);
[scenario2, consistent2, max_depth2] = IA_Network_Check_Consistency(network2, 1, 2, 1);
[scenario3, consistent3, max_depth3] = IA_Network_Check_Consistency(network3, 1, 2, 1);
[scenario4, consistent4, max_depth4] = IA_Network_Check_Consistency(network4, 1, 2, 1);

% Allen's inconsistent example
Ai = IA_Interval('Ai', 1);
Bi = IA_Interval('Bi', 1);
Ci = IA_Interval('Ci', 1);
Di = IA_Interval('Di', 1);

Ri_AB = IA_Relation([0 1 0 0 0 0 0; 0 1 0 0 0 0 0]);
Ri_AC = IA_Relation([0 0 0 0 0 1 0; 0 0 0 0 0 1 0]);
Ri_BC = IA_Relation([0 1 0 0 0 0 0; 0 1 0 0 0 0 0]);
Ri_DA = IA_Relation([0 0 0 1 1 0 0; 0 0 0 0 0 0 0]);
Ri_DB = IA_Relation([0 0 1 0 0 0 0; 0 0 0 0 0 0 0]);
Ri_DC = IA_Relation([0 0 0 1 1 0 0; 0 0 0 0 0 0 0]);

network5 = IA_Network(Ai, Bi, Ri_AB);
network5 = IA_Network_Add_Interval(network5, Ci);
network5 = IA_Network_Add_Interval(network5, Di);

[network5, consistent5] = IA_Network_Add_Relation(network5, 'Di', 'Ai', Ri_DA);
[network5, consistent5] = IA_Network_Add_Relation(network5, 'Di', 'Ci', Ri_DC);
[network5, consistent5] = IA_Network_Add_Relation(network5, 'Di', 'Bi', Ri_DB);
[network5, consistent5] = IA_Network_Add_Relation(network5, 'Bi', 'Ci', Ri_BC);
[scenario5_1, consistent5_1, max_depth5_1] = IA_Network_Check_Consistency(network5, 1, 2, 1);

[network5, consistent5] = IA_Network_Add_Relation(network5, 'Ai', 'Ci', Ri_AC);

[scenario5_2, consistent5_2, max_depth5_2] = IA_Network_Check_Consistency(network5, 1, 2, 1);

%% Our own example

% Sensor one sees the tartgets in order A, B, C, D
A1 = IA_Interval('A1', 1);
B1 = IA_Interval('B1', 1);
C1 = IA_Interval('C1', 1);
D1 = IA_Interval('D1', 1);

% Sensor two sees the targets in order B, C, A, D
B2 = IA_Interval('B2', 1);
C2 = IA_Interval('C2', 1);
A2 = IA_Interval('A2', 1);
D2 = IA_Interval('D2', 1);

%
R_A1_B1 = IA_Relation([1 0 0 0 0 0 0; 0 0 0 0 0 0 0]);
R_B1_C1 = IA_Relation([1 0 0 0 0 0 0; 0 0 0 0 0 0 0]);
R_C1_D1 = IA_Relation([1 0 0 0 0 0 0; 0 0 0 0 0 0 0]);

R_B2_C2 = IA_Relation([1 0 0 0 0 0 0; 0 0 0 0 0 0 0]);
R_C2_A2 = IA_Relation([1 0 0 0 0 0 0; 0 0 0 0 0 0 0]);
R_A2_D2 = IA_Relation([1 0 0 0 0 0 0; 0 0 0 0 0 0 0]);

R_A1_A2 = IA_Relation([0 0 0 0 0 0 1; 0 0 0 0 0 0 1]);
R_B1_B2 = IA_Relation([0 0 0 0 0 0 1; 0 0 0 0 0 0 1]);
R_C1_C2 = IA_Relation([0 0 0 0 0 0 1; 0 0 0 0 0 0 1]);
R_D1_D2 = IA_Relation([0 0 0 0 0 0 1; 0 0 0 0 0 0 1]);

network6 = IA_Network(A1, B1, R_A1_B1);
network6 = IA_Network_Add_Interval(network6, C1);
network6 = IA_Network_Add_Interval(network6, D1);
network6 = IA_Network_Add_Interval(network6, A2);
network6 = IA_Network_Add_Interval(network6, B2);
network6 = IA_Network_Add_Interval(network6, C2);
network6 = IA_Network_Add_Interval(network6, D2);

[temp6, consistent6] = IA_Network_Add_Relation(network6, 'B1', 'C1', R_B1_C1);
if consistent6
  network6 = temp6;
end
[temp6, consistent6] = IA_Network_Add_Relation(network6, 'C1', 'D1', R_C1_D1);
if consistent6
  network6 = temp6;
end
[temp6, consistent6] = IA_Network_Add_Relation(network6, 'B2', 'C2', R_B2_C2);
if consistent6
  network6 = temp6;
end
[temp6, consistent6] = IA_Network_Add_Relation(network6, 'C2', 'A2', R_C2_A2);
if consistent6
  network6 = temp6;
end
[temp6, consistent6] = IA_Network_Add_Relation(network6, 'A2', 'D2', R_A2_D2);
if consistent6
  network6 = temp6;
end

[temp6, consistent6] = IA_Network_Add_Relation(network6, 'B1', 'B2', R_B1_B2);
if consistent6
  network6 = temp6;
end
[temp6, consistent6] = IA_Network_Add_Relation(network6, 'C1', 'C2', R_C1_C2);
if consistent6
  network6 = temp6;
end
[temp6, consistent6] = IA_Network_Add_Relation(network6, 'D1', 'D2', R_D1_D2);
if consistent6
  network6 = temp6;
end
[temp6, consistent6] = IA_Network_Add_Relation(network6, 'A1', 'A2', R_A1_A2);
if consistent6
  network6 = temp6;
end

[scenario6, consistent6, max_depth6] = IA_Network_Check_Consistency(network6, 1, 2, 1);

%% Test some function
relation = IA_Relation_Greater();
[has1] = IA_Relation_Has_Greater(relation);

relation = IA_Relation_Dummy();
[has2] = IA_Relation_Has_Greater(relation);

relation = IA_Relation([1 1 1 1 1 1 1; 0 1 1 1 1 1 1]);
[has3] = IA_Relation_Has_Greater(relation);